<?php
namespace app\index\controller;
use app\index\controller\Base;
use think\Session;
use think\Db;
class Collection extends Base{
// 收藏
    public function index(){
        // return $this->fetch('personal');
    	if(!Session::get('username')){
    		$this->success('请先登陆！','Reg/index');
    	}else{
			$username=Session::get('username');
			$user=db('user')->where('username',$username)->column('id');
			$userid=$user[0];			
			$subQuery = Db::table('keep')
			->where('uid',$userid)
			->buildSql();
			$collects=Db::table('produce')
			->alias('a')
			->join('user b','a.userid = b.id')
			->field('a.pid,title,author,desc,keywords,content,pic,click,thumb,time,b.user_city,b.upic')
			->join([$subQuery=>'c'],
			'a.pid=c.wid','LEFT')->order('a.pid desc')
			->where('c.wid=a.pid')
			->paginate(5);
			$this->assign('collects',$collects);
			$info=db('user')->where('username',$username)->select();
			$this->assign('info',$info);
    		return $this->fetch('personal/collection');
        }
	}
	public function collection(){
    	if(!Session::get('username')){
    		$this->success('请先登陆！','Login/index');
    	}else{
    		$wid=input('sid');
			$username=Session::get('username');
			$user=db('user')->where('username',$username)->column('id');
        	$userid=$user[0];
    		$time=date("Y-m-d H:i:s");
    		$data=[
    			'ctime'=>$time,
    			'uid'=>$userid,
				'wid'=>$wid,
				// 'status'=>$status,
    		];    		
    		if(db('keep')->insert($data)){
    			$this->success('收藏成功！','Index/index');   
    		}
    	}
    }
}